<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="主编号">
          <el-input v-model="queryParams.id" placeholder="主编号"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="queryParams.username" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item label="操作员">
          <el-input v-model="queryParams.operatorName" placeholder="操作员"></el-input>
        </el-form-item>
        <el-form-item label="回收机构">
          <el-input v-model="queryParams.orgName" placeholder="回收机构"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon>
              <Search />
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh />
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table ref="dataTableRef" v-loading="loading" :data="pageData" highlight-current-row border>
        <!-- <el-table-column key="id" label="主编号" prop="id" min-width="150" align="center" /> -->
        <el-table-column key="username" label="用户名" prop="username" min-width="150" align="center" />
        <el-table-column key="operatorName" label="操作员" prop="operatorName" min-width="150" align="center" />
        <el-table-column key="orgName" label="回收机构" prop="orgName" min-width="150" align="center" />
        <el-table-column label="垃圾列表" min-width="150" align="center">
          <template #default="scope">
            <el-link type="primary" icon="Tickets" @click="openWasteListDrawer(scope.row.wasteList)">
              <span style="padding: 0 5px">{{ scope.row.wasteList.length }}项</span>
            </el-link>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button type="primary" size="small" link
              @click="openHistoryDialog(scope.row.requestId, scope.row.processStatus)">
              <template #icon>
                <Document />
              </template>
              查看
            </el-button>
            <el-button type="success" size="small" link @click="handleOpenDialog(scope.row)"
              v-if="scope.row.processStatus != '-1'">
              <template #icon>
                <Plus />
              </template>
              新增
            </el-button>
            <el-button type="danger" size="small" link @click="completed(scope.row.requestId)"
              v-if="scope.row.processStatus != '-1'">
              <template #icon>
                <SwitchButton />
              </template>
              结束
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="handleQuery()" />
    </el-card>
  </div>
  <!--垃圾列表-->
  <el-drawer v-model="wasteListDrawer" :with-header="false" @close="handleCloseWasteDrawer()">
    <el-space v-for="waste in wasteList" :key="waste.id" style="
        width: 100%;
        border: #a9a9a9 1px solid;
        border-radius: 5px;
        padding: 5px;
        margin: 5px 0;
      ">
      <!-- 图片预览 -->
      <el-image :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :preview-src-list="[waste.fileUrl]"
        style="width: 200px; height: 150px" :src="waste.fileUrl" fit="cover" />

      <!-- 垃圾类型选择 -->
      <el-space direction="vertical" :size="10">
        <el-select filterable v-model="waste.typeId" placeholder="请选择垃圾类型" disabled>
          <el-option v-for="item in wasteTypesOptions" :key="item.id" :label="item.typeName" :value="item.id" />
        </el-select>
        <el-input v-model="waste.weight" disabled>
          <template #suffix>千克(kg)</template>
        </el-input>
      </el-space>
    </el-space>
  </el-drawer>
  <!--历史处理记录弹框-->
  <HistoryRecordList ref="historyRecordListRef"></HistoryRecordList>
</template>

<script setup>
import { Delete, Document, Edit, Plus, Refresh } from "@element-plus/icons-vue";

defineOptions({
  name: "ProcessRecord",
  inheritAttrs: false,
});

import ProcessRecordAPI from "@/api/system/process-record";
import RecycleRequestAPI from "@/api/system/recycle-request";
import HistoryRecordList from "@/components/HistoryRecordList/index.vue";

const historyRecordListRef = ref(null)
const queryFormRef = ref();

const loading = ref(false);

const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
});

// 处理记录表格数据
const pageData = ref([]);

/** 查询处理记录 */
function handleQuery() {
  loading.value = true;
  ProcessRecordAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置处理记录查询 */
function handleResetQuery() {
  queryFormRef.value?.resetFields();
  queryParams.pageNum = 1;
  queryParams.id = "";
  (queryParams.username = ""), (queryParams.operatorName = "");
  queryParams.orgName = "";

  handleQuery();
  getHistoryRecord()
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection) {
  removeIds.value = selection.map((item) => item.id);
}

/**
 * 查看垃圾列表抽屉
 */
const wasteListDrawer = ref(false);
const wasteList = ref([]);
const wasteTypesOptions = ref([]);
const openWasteListDrawer = async (list) => {
  wasteListDrawer.value = true;
  wasteTypesOptions.value = await RecycleRequestAPI.getAllWasteTypes();
  if (list?.length) {
    wasteList.value = list;
  }
};

//关闭垃圾列表时回调
const handleCloseWasteDrawer = () => {
  wasteList.value = [];
};

/**
 * 获取历史记录列表
 */
const openHistoryDialog = (requestId, processStatus) => {
  if (processStatus == '-1') {
    historyRecordListRef.value.openHistoryDialog(requestId, 'info');
  } else {
    historyRecordListRef.value.openHistoryDialog(requestId, 'edit');
  }
};

const handleOpenDialog = (row) => {
  historyRecordListRef.value.handleOpenDialog(row, "edit");
}

const completed = (requestId) => {
  ProcessRecordAPI.completed(requestId).then(() => {
    handleQuery();
  });
}
onMounted(() => {
  handleQuery();
});
</script>

<style lang="scss" scoped></style>
